package com.ea.modular.controller.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.ea.common.annotation.LogAnnotation;
import com.ea.common.parent.controller.BaseController;
import com.ea.modular.model.sys.BeanField;
import com.ea.modular.model.sys.GenerateDetail;
import com.ea.modular.model.sys.GenerateInput;
import com.ea.modular.service.sys.GenerateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 代码生成控器
 * 
 * @author jun
 *
 */
@Api(tags = "代码生成")
@Controller
@RequestMapping("/generate")
public class GenerateController extends BaseController {

	private String PREFIX = "/system/code/";
	
	@Autowired
	private GenerateService generateService;


	/**
	 * 跳转到菜单管理首页
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "code";
	}
	
	
    /**
     * 根据角色ID获取授权的菜单
     */
	@ApiOperation("根据表名显示表信息")
    @RequestMapping(value = "/detail/{tableName}")
	@ResponseBody
	public GenerateDetail generateByTableName(@PathVariable String tableName) {
		GenerateDetail detail = new GenerateDetail();
		detail.setBeanName(generateService.upperFirstChar(tableName));
		List<BeanField> fields = generateService.listBeanField(tableName);
		detail.setFields(fields);
		return detail;
	}

	@LogAnnotation
	@ApiOperation(value = "代码生成")
	@RequestMapping(value = "/save")
	@ResponseBody
	public int save(GenerateInput input) {
		System.out.println(input);
		
		generateService.saveCode(input);
		
		return 1;
	}

}
